package cn.onlineclass.service;
import cn.onlineclass.dao.UnitMapper;
import cn.onlineclass.entity.OUnit;
import cn.onlineclass.util.MybatisUtil;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;
import cn.school.service.ProductService;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class UnitService {
    public static final UnitService UNIT=new UnitService();

    //模糊查询
    public AjaxPage search(int pageNo, int pageSize){
        SqlSession s= MybatisUtil.get();
        try {
            UnitMapper mapper = s.getMapper(UnitMapper.class);
            List<OUnit> search = mapper.search(pageNo, pageSize);
            int total = mapper.searchTotal();
            return new AjaxPage(search,total,10,pageNo);
        }finally {
            MybatisUtil.close(s);
        }
    }

    //增加
    public AjaxResult add(OUnit c){
        SqlSession s= MybatisUtil.get();
        try {
            UnitMapper mapper = s.getMapper(UnitMapper.class);
            int add = mapper.add(c);
            return AjaxResult.success(add);
        }finally {
            MybatisUtil.close(s);
        }
    }

    //修改
    public AjaxResult update(OUnit c){
        SqlSession s = MybatisUtil.get();
        try {
            UnitMapper mapper = s.getMapper(UnitMapper.class);
            int update = mapper.update(c);
            return  AjaxResult.success(update);
        }finally {
            MybatisUtil.close(s);
        }
    }

    //删除
    public AjaxResult del(long id){
        SqlSession s =MybatisUtil.get();
        try {
            UnitMapper mapper =s.getMapper(UnitMapper.class);
            int del = mapper.del(id);
            return AjaxResult.success(del);
        }finally {
            MybatisUtil.close(s);
        }
    }

    //根据id查询单个信息
    public AjaxResult get(long id){
        SqlSession s =MybatisUtil.get(true);
        try {
            UnitMapper mapper = s.getMapper(UnitMapper.class);
            OUnit oUnit = mapper.get(id);
            return AjaxResult.success(oUnit);
        }finally {
            MybatisUtil.close(s);
        }
    }
}
